package com.luych.toolbox.base.service.controller;

import com.luych.toolbox.base.service.entity.Role;
import com.luych.toolbox.base.service.service.RoleService;
import com.luych.toolbox.common.feign.intf.base.service.RoleFeign;
import com.luych.toolbox.common.feign.param.base.service.RoleParam;
import com.luych.toolbox.common.feign.view.base.service.RoleView;
import com.luych.toolbox.common.page.CommonPageParam;
import com.luych.toolbox.common.page.CommonPageResult;
import com.luych.toolbox.common.page.CommonSort;
import com.luych.toolbox.common.page.PageResult;
import com.luych.toolbox.common.service.base.BaseController;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "角色")
@RestController
@RequestMapping("/role")
public class RoleController
        extends BaseController<RoleView, RoleParam, Role>
        implements RoleFeign {

    private final RoleService roleService;

    @Autowired
    public RoleController(RoleService roleService) {
        super(roleService);
        this.roleService = roleService;
    }

    @Override
    public CommonPageResult<RoleView> pageByKeyword(long pageSize, long pageNumber, String sort, String order, String keyword) {
        CommonPageParam commonPageParam = CommonPageParam.getInstance().setPageSize(pageSize).setPageNumber(pageNumber);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            commonPageParam.addSort(CommonSort.getInstance().setSort(sort).setOrder(order));
        }
        PageResult<RoleView> pageResult = roleService.pageByKeyword(commonPageParam, keyword);
        return CommonPageResult.getInstance(pageResult);
    }

    @Override
    public void addPermission(String roleId, String permissionId) {
        roleService.addPermission(roleId, permissionId);
    }

    @Override
    public void removePermission(String roleId, String permissionId) {
        roleService.removePermission(roleId, permissionId);
    }
}
